// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/functions/lib/is_null.h>
#include <pollux/functions/lib/registration_helpers.h>
#include <pollux/functions/sparksql/comparisons.h>
#include <pollux/functions/sparksql/least_greatest.h>

namespace kumo::pollux::functions {
    void registerSparkCompareFunctions(const std::string &prefix) {
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_not, prefix + "not");
        registerIsNullFunction(prefix + "isnull");
        registerIsNotNullFunction(prefix + "isnotnull");
    }

    namespace sparksql {
        void registerCompareFunctions(const std::string &prefix) {
            registerSparkCompareFunctions(prefix);
            exec::registerStatefulVectorFunction(
                prefix + "equalto", comparisonSignatures(), makeEqualTo);
            register_function<EqualToFunction, bool, Generic<T1>, Generic<T1> >(
                {prefix + "equalto"});
            exec::registerStatefulVectorFunction(
                prefix + "lessthan", comparisonSignatures(), makeLessThan);
            exec::registerStatefulVectorFunction(
                prefix + "greaterthan", comparisonSignatures(), makeGreaterThan);
            exec::registerStatefulVectorFunction(
                prefix + "lessthanorequal", comparisonSignatures(), makeLessThanOrEqual);
            exec::registerStatefulVectorFunction(
                prefix + "least",
                leastSignatures(),
                makeLeast,
                exec::VectorFunctionMetadataBuilder().defaultNullBehavior(false).build());
            exec::registerStatefulVectorFunction(
                prefix + "greaterthanorequal",
                comparisonSignatures(),
                makeGreaterThanOrEqual);
            // Compare nullsafe functions.
            exec::registerStatefulVectorFunction(
                prefix + "equalnullsafe",
                comparisonSignatures(),
                makeEqualToNullSafe,
                exec::VectorFunctionMetadataBuilder().defaultNullBehavior(false).build());
            register_function<EqualNullSafeFunction, bool, Generic<T1>, Generic<T1> >(
                {prefix + "equalnullsafe"});
            exec::registerStatefulVectorFunction(
                prefix + "greatest",
                greatestSignatures(),
                makeGreatest,
                exec::VectorFunctionMetadataBuilder().defaultNullBehavior(false).build());
            register_function<BetweenFunction, bool, int8_t, int8_t, int8_t>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, int16_t, int16_t, int16_t>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, int32_t, int32_t, int32_t>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, int64_t, int64_t, int64_t>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, int128_t, int128_t, int128_t>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, float, float, float>(
                {prefix + "between"});
            register_function<BetweenFunction, bool, double, double, double>(
                {prefix + "between"});
            // Decimal comapre functions.
            POLLUX_REGISTER_VECTOR_FUNCTION(
                udf_decimal_gt, prefix + "decimal_greaterthan");
            POLLUX_REGISTER_VECTOR_FUNCTION(
                udf_decimal_gte, prefix + "decimal_greaterthanorequal");
            POLLUX_REGISTER_VECTOR_FUNCTION(udf_decimal_lt, prefix + "decimal_lessthan");
            POLLUX_REGISTER_VECTOR_FUNCTION(
                udf_decimal_lte, prefix + "decimal_lessthanorequal");
            POLLUX_REGISTER_VECTOR_FUNCTION(udf_decimal_eq, prefix + "decimal_equalto");
            POLLUX_REGISTER_VECTOR_FUNCTION(
                udf_decimal_neq, prefix + "decimal_notequalto");
        }
    } // namespace sparksql
} // namespace kumo::pollux::functions
